package com.chujian.Adapter;

import java.util.List;
import java.util.Map;

import com.chujian.orderingsystem.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chujian.Activities.YixuanCaipinActivity;
import com.chujian.Global.Global;
import com.chujian.model.Food;
import com.chujian.model.ItemBean;
/**
 * 已选菜品（购物车）的适配器
 * @author cc
 *
 */
public class YiXuanAdapter extends BaseAdapter  {
	private LayoutInflater mLayoutInflater;
	private List<ItemBean> mDataList;
	private Context context;
	private TextView tvTotalPrice;
	
//	Map<Food, Integer>map;
	
	
	public YiXuanAdapter( Context context, List<ItemBean> mDataList,TextView tvTotalPrice) {
		super();
		mLayoutInflater = LayoutInflater.from(context);
		this.mDataList = mDataList;
		this.context=context;
		this.tvTotalPrice=tvTotalPrice;
	}

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mDataList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder=null;
		if(convertView==null){
			holder=new ViewHolder();
			convertView=mLayoutInflater.inflate(R.layout.yixuan_caipin_item, null);
			holder.btnAdd=(Button) convertView.findViewById(R.id.btn_add);
			holder.btnSub=(Button) convertView.findViewById(R.id.btn_sub);
			holder.etCount=(EditText) convertView.findViewById(R.id.edt_count);
			holder.tvName=(TextView) convertView.findViewById(R.id.tv_caipin);
			holder.tvPrice=(TextView) convertView.findViewById(R.id.tv_price);
			convertView.setTag(holder);
			
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		final ItemBean bean=mDataList.get(position);
		holder.tvName.setText(bean.food.getFoodName());
		holder.tvPrice.setText(bean.food.getFoodPrice()+"");
		holder.etCount.setText(bean.count+"");
		
		final EditText etCount=holder.etCount;
//		添加按钮监听
		holder.btnAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Toast.makeText(context, "add", Toast.LENGTH_SHORT).show();
				
				if(Global.selectedFood.containsKey(bean.food)){
					   Global.count++;
					   Integer foodCount=Global.selectedFood.get(bean.food);
					   Global.selectedFood.put(bean.food, ++foodCount);
					   Global.tatalPrice+=bean.food.getFoodPrice();
					   tvTotalPrice.setText("合计："+Global.tatalPrice+"元");
					   etCount.setText(foodCount+"");
					}
				
			}
		});
//		减少按钮的监听
		holder.btnSub.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Toast.makeText(context, "sub", Toast.LENGTH_SHORT).show();
				Integer foodCount=0;
				if(Global.selectedFood.containsKey(bean.food)){
					   foodCount=Global.selectedFood.get(bean.food);
					   if(foodCount>0){
						   Global.selectedFood.put(bean.food, --foodCount);
						   Global.count--;
						   etCount.setText(foodCount+"");
						   Global.tatalPrice-=bean.food.getFoodPrice();
						   tvTotalPrice.setText("合计："+Global.tatalPrice+"元");
					   }
					  
					   if(foodCount<=0){
						   Global.selectedFood.remove(bean.food);
						   mDataList.remove(position);
						   notifyDataSetChanged();
					   }
//					   
					}
			}
		});
		
		
		return convertView;
	}
	
	class ViewHolder{
		public TextView tvName;
		public TextView tvPrice;
		public Button btnAdd;
		public EditText etCount;
		public Button btnSub;
		
	}


}
